package com.yanqu.road.server.cmd.activity.suoyao;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.suoyao.SuoYaoUserData;
import com.yanqu.road.pb.activity.SuoYaoActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.suoyao.SuoYaoModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.suoyao.SuoYaoMgr;
import com.yanqu.road.server.pb.SuoYaoPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SUOYAO_ACTIVITY_EXPLORE_TREASURE, desc = "锁妖塔探宝")
public class SuoYaoExploreTreasureCmd extends AbstractCommand {

    // 1普通探宝，2快速探宝
    private int type;

    private SuoYaoUserData userData;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SuoYaoActivityProto.SuoYaoExploreReqMsg reqMsg = SuoYaoActivityProto.SuoYaoExploreReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = SuoYaoMgr.getActivityInfo();
        if (!ActivityMgr.activityInTimeAndShowTime(activityInfo)) {
            ret = GameErrorCode.E_SUOYAO_ACTIVITY_NO_OPEN;
            return;
        }
        SuoYaoModule module = player.getModule(SuoYaoModule.class);
        userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SUOYAO_ACTIVITY_NO_UNLOCK;
            return;
        }

        ret = module.exploreTreasure(type == 2);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            SuoYaoActivityProto.SuoYaoExploreRespMsg.Builder builder = SuoYaoActivityProto.SuoYaoExploreRespMsg.newBuilder();
            builder.setRet(ret);
            if (userData != null) {
                builder.setUserData(SuoYaoPb.parseSuoYaoUserDataMsg(userData));
            }
            player.sendPacket(ClientProtocol.U_SUOYAO_ACTIVITY_EXPLORE_TREASURE, builder);
        }
    }
}
